package com.chaoyue.learning.service.impl;

import com.alibaba.fastjson.JSON;

import com.chaoyue.base.exception.ChaoYuePlusException;
import com.chaoyue.learning.config.PayNotifyConfig;
import com.chaoyue.learning.service.MyCourseTablesService;
import com.chaoyue.messagesdk.model.po.MqMessage;
import com.chaoyue.messagesdk.service.MqMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class ReceivePayNotifyService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    MqMessageService mqMessageService;

    @Autowired
    MyCourseTablesService myCourseTablesService;


    //监听消息队列接收支付结果通知
    @RabbitListener(queues = PayNotifyConfig.PAYNOTIFY_QUEUE)
    public void receive(Message message) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        byte[] body = message.getBody();
        String jsonString = new String(body);
        MqMessage mqMessage = JSON.parseObject(jsonString, MqMessage.class);
        //选课记录id
        String choseCourseId = mqMessage.getBusinessKey1();
        //业务类型
        String orderType = mqMessage.getBusinessKey2();
        if(orderType.equals("60201")) {
            boolean b = myCourseTablesService.saveChooseCourseStatus(choseCourseId);
            if(!b){
                ChaoYuePlusException.cast("保存选课记录失败");
            }
        }
    }


}

